/**
 * 转发注册
 * @param that(指向vue)
 */
export function wxconfig(that) {
  var url = 'fiveques/gethasshare/?op_id=' + window.localStorage.opt_id;
  var link = 'http://askh5.lightway.cc/?';
  if (that.$route.query.exam_id !== 'undefined' && that.$route.query.exam_id) {
    url = url + '&exam_id=' + that.$route.query.exam_id;
    link = link + 'exam_id=' + that.$route.query.exam_id;
  } else {
    url = url + '&exam_id=' + 2;
    link = link + 'exam_id=' + 2;
  }
  if (that.$route.query.source !== 'undefined' && that.$route.query.source) {
    url = url + '&source=' + that.$route.query.source;
    link = link + '&source=' + that.$route.query.source;
  }

  if (that.$route.query.spread_level !== 'undefined' && that.$route.query.spread_level) {
    link = link + '&spread_level=' + (parseInt(that.$route.query.spread_level) + 1);
  } else {
    link = link + '&spread_level=2';
  }
  if (window.localStorage.opt_id) {
    that.$http.get('getjsconfig/?opt_id=' + window.localStorage.opt_id + '&url=' + escape(window.location.href.split('#')[0])).then((response) => {
      window.wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: response.data.data.appId, // 必填，公众号的唯一标识
        timestamp: response.data.data.timestamp, // 必填，生成签名的时间戳
        nonceStr: response.data.data.noncestr, // 必填，生成签名的随机串
        signature: response.data.data.signature, // 必填，签名，见附录1
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
      });
      window.wx.ready(function() {
        var title = response.data.data.title;
        var desc = '题目全部答中，即获得500元红包哦!';
        window.wx.onMenuShareTimeline({
          title: title, // 分享标题
          link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: 'http://askactivity.lightway.cc/static/title_img/askfive_fengmian.jpg', // 分享图标
          success: function () {
            that.$http.get(url).then((response) => {});
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        });
        window.wx.onMenuShareAppMessage({
          title: title, // 分享标题
          desc: desc, // 分享描述
          link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: 'http://askactivity.lightway.cc/static/title_img/askfive_fengmian.jpg', // 分享图标
          type: '', // 分享类型,music、video或link，不填默认为link
          dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
          success: function () {
            that.$http.get(url).then((response) => {
              that.shareprice = response.body.data.price;
            });
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        });
      });
    });
  }
}

/**
 * 获取openid
 * @param that(指向vue)
 * @param redirecturi(回掉地址)
 * @param appid
 * @callback 成功回调函数
 */
export function getopenid (that, redirecturi, appid) {
  const ERR_OK = 1;
  let url = redirecturi;
  if (!window.localStorage.opt_id && !that.$route.query.code) {
    window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8fe2a003c0b6b26f&redirect_uri=' + url + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
  }
  if (window.localStorage.opt_id === 'undefined') {
    window.localStorage.removeItem('opt_id');
    window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8fe2a003c0b6b26f&redirect_uri=' + url + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
  }
  if (that.$route.query.code) {
    that.$http.get('getopid?code=' + that.$route.query.code).then((response) => {
      response = response.body;
      if (response.errno === ERR_OK) {
        window.localStorage.opt_id = response.data.openid;
      } else {
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8fe2a003c0b6b26f&redirect_uri=' + url + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
      }
    });
  }
}
